
#include "InvoicesModel.h"
#include "Customer.h"
#include "Helper.h"

#include <QBrush>
#include <QFile>
#include <assert.h>
#include <QDebug>

using namespace ecocity;

const int InvoicesModel::s_columns = 8;

InvoicesModel::InvoicesModel( const std::vector< Invoice >& invoices,
                              QObject *parent )
    : QStandardItemModel( parent )
{
    for ( size_t row = 0; row < invoices.size( ); ++row )
    {
        add_invoice( invoices[ row ] );
    }
}

//---------------------------------------------------------------

void
InvoicesModel::reset( const std::vector< Invoice >& invoices )
{
    removeRows( 0, rowCount() );

    for ( size_t row = 0; row < invoices.size( ); ++row )
    {
        add_invoice( invoices[ row ] );
    }
}

//---------------------------------------------------------------

QVariant
InvoicesModel::headerData( int section, Qt::Orientation orientation, int role ) const
{
    if ( role != Qt::DisplayRole || orientation != Qt::Horizontal )
    {
        return QVariant();
    }

    switch( section )
    {
    case INVOICE_ID:
        return QString( "Numero Factura" );

    case INVOICE_DATE:
        return QString( "Fecha" );

    case INVOICE_CUSTOMER_NAME:
        return QString( "Cliente" );

    case INVOICE_PRODUCT_NAMES:
        return QString( "Productos" );

    case INVOICE_TOTAL_TAXES:
        return QString( "Total IVA" );

    case INVOICE_TOTAL_WITHOUT_TAX:
        return QString( "Precio sin I.V.A." );

    case INVOICE_TOTAL_PROFIT:
        return QString( " Beneficio Total" );

    case INVOICE_TOTAL_PRICE:
        return QString( "Precio Total" );

    default:
        return QVariant();
    }
}

//---------------------------------------------------------------

Qt::ItemFlags
InvoicesModel::flags ( const QModelIndex& /*index*/ ) const
{
    return Qt::ItemIsEnabled | Qt::ItemIsSelectable; // No editable items
}

//---------------------------------------------------------------

void
InvoicesModel::save( const QString &file_name ) const
{
    if( file_name.isEmpty( ) )
    {
        return;
    }

    QFile file( file_name );

    if ( !file.open( QIODevice::WriteOnly ) )
    {
        return;
    }

    QTextStream out( &file );

    for ( int row = 0; row < rowCount(); ++row )
    {
        for ( int column = 0; column < s_columns; ++column )
        {
            switch (column) {
            case INVOICE_ID:
                out << item( row, column )->data( Qt::EditRole ).toInt();
                break;
            case INVOICE_TOTAL_TAXES:
            case INVOICE_TOTAL_WITHOUT_TAX:
            case INVOICE_TOTAL_PROFIT:
            case INVOICE_TOTAL_PRICE:
                out << item( row, column )->data( Qt::EditRole ).toFloat();
                break;
            case INVOICE_CUSTOMER_NAME:
            case INVOICE_DATE:
            case INVOICE_PRODUCT_NAMES:
                out << item( row, column )->text();
                break;
            default:
                break;
            }

            if ( column < s_columns - 1 )
            {
                out << ",";
            }
        }

        out << "\n";
    }

    file.close();
}

//---------------------------------------------------------------

bool
InvoicesModel::add_invoice( const Invoice& invoice )
{
    QList<QStandardItem*> items;

    QStandardItem* invoice_id = new QStandardItem;
    QStandardItem* customer_name = new QStandardItem;
    QStandardItem* total_taxes = new QStandardItem;
    QStandardItem* total_without_taxes = new QStandardItem;
    QStandardItem* profit = new QStandardItem;
    QStandardItem* total_cost = new QStandardItem;

    invoice_id->setData( invoice.id( ), Qt::EditRole );
    customer_name->setData( invoice.customer( ).name(), Qt::EditRole );
    total_cost->setData( QString::number( invoice.total_cost(), 'f', 2 ), Qt::EditRole );
    total_taxes->setData( QString::number( invoice.taxes_cost( ), 'f', 2 ), Qt::EditRole );
    total_without_taxes->setData( QString::number( invoice.cost_without_taxes( ), 'f', 2 ), Qt::EditRole );
    profit->setData( QString::number( invoice.profit( ), 'f', 2 ), Qt::EditRole );

     QString product_names = Helper::get_product_names( invoice.items( ) ).join( "," );

    items << invoice_id
          << new QStandardItem( invoice.date( ).toString( "dd.MM.yyyy" ) )
          << customer_name
          << new QStandardItem( product_names )
          << total_taxes
          << total_without_taxes
          << profit
          << total_cost;

    if( items.size() != s_columns )
    {
        // Ensure you don't forget anything
        assert( 0 );
        return false;
    }

    insertRow( rowCount( ), items );

    return true;
}

//---------------------------------------------------------------

int
InvoicesModel::get_biggest_id( )
{
    int max_id = -1;

    for ( int i = 0; i < rowCount( ); ++i )
    {
        int id = data( index( i, INVOICE_ID ) ).toInt( );

        if ( id > max_id )
        {
            max_id = id;
        }
    }

    return max_id;

}
